package com.yzkj.framework.core.filter;

import cn.hutool.json.JSONUtil;
import com.yzkj.framework.beans.result.ApiResponse;
import com.yzkj.framework.core.util.RSAUtils;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 统一返回
 */
@Component
@ControllerAdvice
@SuppressWarnings("rawtypes")
class ResponseAdvice implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        //这个地方如果返回false ,不会执行 beforeBodyWrite 方法
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {

        if (o == null) {
            return ApiResponse.success(null);
        }

        return o instanceof ApiResponse ? o : ApiResponse.success(o);
    }
}
